package com.util;

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import java.util.Date;
import java.util.Locale;
import java.text.SimpleDateFormat;


import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * User: IBM
 * Date: 2009-7-28
 * Time: 14:57:47
 */
public class YahooHandler extends DefaultHandler {
     private Date weatherDate;
    private String low;
    private String high;
    private String code;

    public void startElement(String uri, String localName, String qName, Attributes attributes)

throws SAXException {
        if("yweather:condition".equals(qName)) {
            String s_date = attributes.getValue(3);
            try {
                Date publish = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm a z", Locale.US).parse(s_date);
                //System.out.println("Publish: " + publish.toString());
            }
            catch (Exception e) {
                e.printStackTrace();
                throw new SAXException("Cannot parse date: " + s_date);
            }
        }
        else if("yweather:forecast".equals(qName)) {
            String s_date = attributes.getValue(1);
            Date date = null;
            try {
                date = new SimpleDateFormat("dd MMM yyyy", Locale.US).parse(s_date);
            }
            catch (Exception e) {
                e.printStackTrace();
                throw new SAXException("Cannot parse date: " + s_date);
            }
             int low = Integer.parseInt(attributes.getValue("low"));
            int high = Integer.parseInt(attributes.getValue("high"));
            String text = attributes.getValue("text");
            int code = Integer.parseInt(attributes.getValue("code"));
            System.out.println("Weather: "+ text + ", low=" + low + ", high=" + high+",code="+code);
            this.low=String.valueOf(low);
            this.high=String.valueOf(high);
            this.code=String.valueOf(code);
            return;
        }
        super.startElement(uri, localName, qName, attributes);
    }

    public Date getWeatherDate() {
        return weatherDate;
    }

    public String getLow() {
        return low;
    }

    public String getHigh() {
        return high;
    }

    public String getCode() {
        return code;
    }
}
